<template>
  <ex-dialog
    width="900px"
    class="ex-diolog"
    :title="title"
    :visible="visible"
    @closeDialog="handleClose" 
  >	
		<el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm" :disabled="disabled">
      <el-form-item label="活动名称" prop="activityName">
				<el-input type="text" v-model="ruleForm.activityName" autocomplete="off" placeholder="请输入活动名称"></el-input>
			</el-form-item>
      <el-form-item label="兑换时间" prop="date">
        <el-date-picker
          v-model="ruleForm.date"
          type="datetimerange"
          format="yyyy-MM-dd HH" value-format="yyyy-MM-dd HH" 
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
			</el-form-item>
      <el-form-item label="兑换数量:" prop="exchangeNum">
				<el-input type="text" v-model="ruleForm.exchangeNum" autocomplete="off" placeholder="请输入兑换数量"></el-input>
			</el-form-item>
      <el-form-item label="兑换费率:" prop="exchangeServiceRatio">
        <el-input placeholder="请输入兑换费率" v-model="ruleForm.exchangeServiceRatio" autocomplete="off">
          <template slot="append">%</template>
        </el-input>
			</el-form-item>
      <el-form-item label="兑换比例:">
				<div class="inputBox">
          <el-form-item label="" prop="rate">
            <el-input type="text" v-model="ruleForm.rate" autocomplete="off" placeholder="CoCo" :disabled="true"></el-input>
          </el-form-item>
          <p class="dian">:</p>
          <el-form-item label="" prop="exchangeRatio">
            <el-input type="text" v-model="ruleForm.exchangeRatio" autocomplete="off" placeholder="椰分"></el-input>
          </el-form-item>
        </div>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" @click="draftForm('ruleForm')" :loading="loading">确认提交</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')" :loading="loading" v-if="ruleForm.status != 2">保存草稿</el-button>
				<el-button @click="handleClose('ruleForm')">取消</el-button>
			</el-form-item>
		</el-form>
  </ex-dialog>
</template>
<script>
// components
import Loading from '@/components/Loading/index'
// api
import {
  addExchange,
  editExchange,
  exchangeDetail,
  commitExchange
} from '@/api/integralOperation/changeConfig'
import '@wangeditor/editor/dist/css/style.css'
import {
  Editor,
  Toolbar
} from "@wangeditor/editor-for-vue";
export default {
  components: {
    Loading,
    Editor,
    Toolbar
  },
  props: {
    visible: Boolean,
    id: String,
    pathType: String,
  },
  data() {
    var validateMinBetAmount= (rule, value, callback) => {
      var reg = /(^[1-9]\d*$)/
      if (value === '') {
        callback(new Error('填写最低下注金额'));
      } else if (!reg.test(value)) {
        callback(new Error('请填写大于0的整数'));
      }  else {
        callback();
      }
    };
    return {
      disabled: false,
      loading: false,
      title: '新增兑换配置',
      betTypeList: [
        {id: '1',value: '神椰coco'}
      ],
      typeList: [
        {id: '1',value: '支持竞猜'},
        {id: '2',value: '冠军竞猜'},
      ],
      roleList: [],
			ruleForm: {
        id: '',//		id
				activityName: '',//活动名称
				exchangeNum: '',//兑换数量
        exchangeRatio: '',//	兑换比例-分母
        exchangeServiceRatio: null,//兑换手续费
        exchangeTimeEnd: '',//	兑换时间结束
        exchangeTimeStart: null,//	兑换时间开始
        date: [],
        rate: 1,
        status: '',
      },
      flags: false,
			rules: {
        activityName:   [
          { required: true,message: '请输入活动名称',trigger: 'blur' }
        ],
        exchangeNum: [
          { required: true,message: '请输入兑换数量',trigger: 'blur' }
        ],
        rate: [
          { required: true,message: '请输入',trigger: 'blur' }
        ],
        exchangeRatio: [
          { required: true,message: '请输入椰分',trigger: 'blur' }
        ],
        exchangeServiceRatio: [
          { required: true,message: '请输入兑换费率', trigger: 'blur' }
        ],
        date: [
          { required: true,message: '请选择时间',trigger: 'change' }
        ],
      },
    }
  },
  computed: {
   
  },
  created() {
    var num = -152+50+50+50+100+50+400+500+
    console.log('num',num)
    if(this.pathType == 'edit'){
      this.title = '编辑兑换配置'
      setTimeout(() => {
        this.getDetail()
      }, 100)
    } else if(this.pathType == 'add'){
      this.ruleForm.id = ''
    } else if(this.pathType == 'look'){
      this.disabled = true
       this.title = '查看兑换配置'
       setTimeout(() => {
        this.getDetail()
      }, 100)
    }   

	},
  methods: {
    //活动详情
    getDetail(){
      var id = this.id
      console.log('id',id)
      exchangeDetail(id).then(res => {
        this.ruleForm = res.result
        this.ruleForm.date = [res.result.exchangeTimeStart,res.result.exchangeTimeEnd]
        this.ruleForm.text = res.result.text
        this.ruleForm.rate = 1
      })
    },
    //新建子账号
    submitForm(formName) {
      console.log('formName',formName)
      this.$refs[formName].validate((valid) => {
        console.log('valid',valid)
        if (valid) {
          this.loading = true
          this.ruleForm.exchangeTimeStart = this.ruleForm.date[0]
          this.ruleForm.exchangeTimeEnd = this.ruleForm.date[1]
          if(this.pathType == 'add'){
            var data = {
              ...this.ruleForm
            }
            console.log('data',data)
            addExchange(data).then(res => {
              this.handleSuccess("创建成功")
              setTimeout(() => {
                this.loading = false
              }, 1000);
            }).catch(()=>{
               this.loading = false
            }).finally(() => {

            })
          }else if(this.pathType == 'edit'){
            var data = {
              ...this.ruleForm
            }
            editExchange(data).then(res => {
              this.handleSuccess("修改成功")
              setTimeout(() => {
                this.loading = false
              }, 1000);
            }).catch(()=>{
               this.loading = false
            }).finally(() => {
            })
          }
           
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    /* 保存草稿 */
    draftForm(formName) {
      console.log('formName',formName)
      this.$refs[formName].validate((valid) => {
        console.log('valid',valid)
        if (valid) {
          this.loading = true
          this.ruleForm.exchangeTimeStart = this.ruleForm.date[0]
          this.ruleForm.exchangeTimeEnd = this.ruleForm.date[1]
          var data = {
            ...this.ruleForm
          }
          console.log('data',data)
          commitExchange(data).then(res => {
            this.handleSuccess("创建成功")
            setTimeout(() => {
              this.loading = false
            }, 1000);
          }).catch(()=>{
              this.loading = false
          }).finally(() => {

          })
           
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    
		//取消
    handleClose(formName) {
      this.$emit('update:visible', false)
			// this.$refs[formName].resetFields();
    },
		//确认
		handleConfirm(){
			this.$emit('success', '你好')
		},
    handleSuccess(msg) {
      if (msg === '已拒绝') {
        var type = 'error'
      } else {
        var type = 'success'
      }
      this.$message({
        message: msg,
        type: type,
        duration: 1000,
        onClose: () => {
          this.handleClose()
          this.$emit('success')
        }
      })
    },
  }
}
</script>
<style lang="scss" scoped>
.ex-diolog {
  ::v-deep .ex--dialog--footer {
    border: none !important;
		padding: 0 !important;
  }
	::v-deep .ex--dialog--main {
    padding: 10px 20px !important; 
  }
}
::v-deep .avatar-uploader2 .el-upload {
  width: 100% !important;
}

.avatar-uploader2 {
  width: 130px;
  height: 130px;
  text-align: center;
  line-height: 130px;
  background-color: #fbfdff;
  border: 1px dashed #c0ccda;
}

.banner_img {
  width: 130px;
  height: 130px;
}

.richText {
  border: 1px solid #ccc;
  line-height: normal;
}
.inputBox{
  display: flex;
  justify-content: flex-start;
  align-items: center;
  .dian{
    width: 20px;
    text-align: center;
  }
  .el-input{
    width: 140px;
  }
}
.el-input{
  width: 300px;
}
</style>